package com.mvu.banana.internal.server;

import com.mvu.banana.domain.impl.KeyableBean;
import com.mvu.banana.domain.server.BlockReturn;
import org.apache.log4j.Logger;

import javax.persistence.EntityManager;
import java.util.List;

/**
 */
public abstract class BuilderBase<T extends KeyableBean>{
  protected static final Logger logger = Logger.getLogger(BuilderBase.class);
  protected T product;

  public BuilderBase(T product){
    this.product = product;
  }

  public T commit(){
    product.persist();
    return product;
  }

  public List<T> list(){
    return new BlockReturn<List<T>>(){
      @Override
      public List<T> run(EntityManager em) {
        return em.createQuery("SELECT t FROM " + product.getClass().getSimpleName() + " t")
          .getResultList();
      }
    }.run();
  }
}
